package company.tools.editor;

import java.nio.file.Files

import org.iweb.html.api.GroovyListener
import org.iweb.html.impl.basic.Button
import org.iweb.html.impl.basic.Div
import org.iweb.html.impl.basic.Pre
import org.iweb.html.impl.basic.Script
import org.iweb.html.impl.basic.TextArea
import org.iweb.html.impl.basic.Form.CustomFormBean
import org.iweb.html.impl.js.engine.JavascriptEngine
import org.iweb.web.ui.page.Page;

@Page(path="/ciccio2",layout="ace-layout")
class Index{
	
	Pre pre=new Pre();
	
	def run(JavascriptEngine jsEngine,CustomFormBean bean){		
		jsEngine.textEditor("import iweb.api.html.impl.basic.Button;");
	}
	
	@Override
	public Object render() {
		
		Div masterRow=new Div();		
		masterRow.setClassStyle("row-fluid");
		
		Div container=new Div();
		container.setClassStyle("span12");
		
		masterRow.append(container);
		
		Button b =new Button("Change text");
		
		b.onClick(new GroovyListener("editor-change", this, "run"));
		
		container.append(b);
		
		String s="import iweb.api.html.impl.basic.Button;import iweb.api.html.impl.basic.Div;import iweb.api.html.impl.basic.Form.CustomFormBean;import iweb.api.html.impl.basic.Head;import iweb.api.html.impl.basic.Head.DefaultScript;import iweb.api.html.impl.basic.Html;import iweb.api.html.impl.basic.Head.Meta;import iweb.api.html.impl.basic.Script;import iweb.api.html.impl.basic.Style;import iweb.api.html.impl.basic.TextArea;import iweb.api.html.impl.js.engine.JavascriptEngine;import iweb.api.web.page.AbstractPage;import iweb.api.html.api.GroovyListener;import iweb.api.html.impl.basic.Span;class Index extends AbstractPage {def run(JavascriptEngine jsEngine,CustomFormBean bean){//jsEngine.show(toShow);}@Override public Object render() {Div masterRow=new Div();masterRow.setClassStyle(\"row-fluid\");Div container=new Div();container.setClassStyle(\"span12\");masterRow.append(container);TextArea ta=new TextArea(\"\");container.append(ta);container.append(new Script().setText(\"tinymce.init({selector: \"textarea\",theme: \"modern\", height: 500,plugins: [],toolbar: \"undo redo | outdent indent \",menubar : false});\"));return masterRow;}}"
		
		File f = new File(System.getProperty("catalina.base")+"/webapps/iweb/template/company/tools/editor/Index.groovy");
		byte[] bytes = Files.readAllBytes(f.toPath());
		s =new String(bytes,"UTF-8");
		
		TextArea ta=new TextArea(s);
		ta.setId("editor");
		
		//container.append(ta);
		
		//Pre p=new Pre();
		pre.setId("editor");
		pre.setText(s);
		//pre.css("height", "100%");
		pre.css("margin","0");
        pre.css("position","absolute");
        pre.css("top","0");
        pre.css("bottom","0");
        pre.css("left","0");
        pre.css("right","0");
		container.append(pre);
		
		//container.append(new Script().setText("tinymce.init({selector: \"textarea\",theme: \"modern\", height: 500,plugins: [\"code  bbcode save\"],toolbar: \"undo redo | outdent indent \",menubar : false});"));		
		
		container.append(new Script().setText("ace.require('ace/ext/language_tools'); var aceEditor = ace.edit('editor');aceEditor.setTheme('ace/theme/eclipse');aceEditor.getSession().setMode('ace/mode/groovy');aceEditor.setOptions({enableBasicAutocompletion: true});"));
		
		return masterRow;
		
	}
	
	

}